﻿using MyHome.Configuration.Net;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using ViewModelBase;

namespace MyHome.Configuration.ViewModel
{
    public class PluginConnectionViewModel : ViewModelBase.ViewModel<PluginConnectionTemplate>
    {
        public PluginViewModel Input { get; set; }

        public PluginViewModel Output { get; set; }

        public int X1
        {
            get
            {
                return Input.X + 200;
            }
        }

        public int X2
        {
            get
            {
                return Output.X;
            }
        }

        public int Y1
        {
            get
            {
                return Input.Y + 50;
            }
        }

        public int Y2
        {
            get
            {
                return Output.Y + 50;
            }
        }

        #region Selected property

        private bool isSelected;

        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                if (IsSelected != value)
                {
                    isSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        #endregion Selected property

        #region DeleteCommand

        private ICommand deleteCommand;

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                    deleteCommand = new RelayCommand(async param => await ExecuteDeleteCommand(param));

                return deleteCommand;
            }
        }

        private async Task ExecuteDeleteCommand(object param)
        {
            var result = MessageBox.Show("Are you sure?", "", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                host.PluginConnections.Remove(this);
                host.SelectedPluginConnection = null;
            }
        }

        #endregion DeleteCommand

        private PluginHostViewModel host;

        public PluginConnectionViewModel(PluginConnectionTemplate model, PluginHostViewModel host)
            : base(model)
        {
            this.host = host;
            Input = host.Plugins.Single(p => p.Id == Model.InputId);
            Output = host.Plugins.Single(p => p.Id == Model.OutputId);
            Input.PropertyChanged += Input_PropertyChanged;
            Output.PropertyChanged += Output_PropertyChanged;
        }

        private void Input_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "X")
                OnPropertyChanged("X1");
            else if (e.PropertyName == "Y")
                OnPropertyChanged("Y1");
        }

        private void Output_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "X")
                OnPropertyChanged("X2");
            else if (e.PropertyName == "Y")
                OnPropertyChanged("Y2");
        }
    }
}